// src/services/auditorService.js

import { myRequest } from '@/utils/request';

export var auditorService = {
  /**
   * 获取待审核的通用学分认证列表
   * @param {number} page - 页码
   * @param {number} size - 每页数量
   * @returns {Promise<any>}
   */
  getPendingCertifications: function(page = 1, size = 10) {
    return new Promise((resolve, reject) => {
      myRequest.request({
        url: `auditors/certifications/pending?page=${page}&size=${size}`,
        method: 'GET',
        success: (res) => {
          resolve(res);
        },
        error: (err) => {
          reject(err);
        }
      });
    });
  },

  /**
   * 批准/拒绝学分认证（通用及课程认证）
   * @param {string} certificationId - 认证申请ID
   * @param {number} auditStatus - 1 (批准) 或 2 (拒绝)
   * @param {number} credits - 要授予的学分数量 (课程认证时此参数将被忽略)
   * @param {string} applicantUserId - 申请人（学生）的用户ID
   * @returns {Promise<any>}
   */
  auditCertification: function(certificationId, auditStatus, credits, applicantUserId) {
    return new Promise((resolve, reject) => {
      // 手动构建查询参数字符串
      const queryParams = new URLSearchParams({
        auditStatus: auditStatus,
        credits: credits,
        applicantUserId: applicantUserId
      }).toString();

      // 将查询参数直接拼接到 URL 上，因为 request.js 不会自动处理 PUT 请求的 param.params
      const urlWithParams = `auditors/certifications/${certificationId}/audit?${queryParams}`;

      myRequest.request({
        url: urlWithParams, // 使用手动拼接参数的URL
        method: 'PUT',
        // 这里不再需要 params 属性，因为参数已经包含在 URL 中
        // params: { ... }
        success: (res) => {
          resolve(res);
        },
        error: (err) => {
          reject(err);
        }
      });
    });
  },

  /**
   * 获取待审核的课程学分认证列表 (新功能)
   * @param {number} page - 页码
   * @param {number} size - 每页数量
   * @returns {Promise<any>}
   */
  getPendingCourseCertifications: function(page = 1, size = 10) {
    return new Promise((resolve, reject) => {
      myRequest.request({
        url: `auditors/course-certifications/pending?page=${page}&size=${size}`,
        method: 'GET',
        success: (res) => {
          resolve(res);
        },
        error: (err) => {
          reject(err);
        }
      });
    });
  }
};